<template>
	<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
		<a-form-item label="发送方式：" name="sendType">
			<a-radio-group v-model:value="sendType">
				<a-radio value="TXT">纯文本</a-radio>
				<a-radio value="HTML">HTML</a-radio>
			</a-radio-group>
		</a-form-item>
		<a-form-item label="接收人邮箱：" name="receiverMail">
			<a-textarea
				v-model:value="formData.receiverMail"
				placeholder="请输入接收人邮箱，多个逗号拼接"
				:auto-size="{ minRows: 2, maxRows: 5 }"
			/>
		</a-form-item>
		<a-form-item label="邮件主题：" name="mailSubject">
			<a-input v-model:value="formData.mailSubject" placeholder="请输入邮件主题" allow-clear />
		</a-form-item>
		<a-form-item label="邮件正文" name="mailBody" v-if="sendType === 'TXT'">
			<a-textarea
				v-model:value="formData.mailBody"
				placeholder="请输入邮件正文"
				:auto-size="{ minRows: 6, maxRows: 6 }"
			/>
		</a-form-item>
		<a-form-item label="邮件正文" name="mailBody" v-if="sendType === 'HTML'">
			<xn-editor v-model="formData.mailBody" placeholder="请输入邮件正文" :height="200"></xn-editor>
		</a-form-item>
	</a-form>
</template>

<script setup name="localEmailSend">
	import { message } from 'ant-design-vue'
	import XnEditor from '@/components/Editor/index.vue'
	import { required } from '@/utils/formRules'
	import emailApi from '@/api/dev/emailApi'

	// 发送文本方式
	const sendType = ref('TXT')
	// 定义emit事件
	const emit = defineEmits({ loadingStart: null, loadingEnd: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})

	// 默认要校验的
	const formRules = {
		receiverMail: [required('请输入接收人邮箱，多个逗号拼接')],
		mailSubject: [required('请输入邮件主题')],
		mailBody: [required('请输入邮件正文')]
	}
	// 发送
	const send = () => {
		formRef.value
			.validate()
			.then(() => {
				emit('loadingStart')
				if (sendType.value === 'TXT') {
					return emailApi.emailSendLocalTxt(formData.value)
				} else if (sendType.value === 'HTML') {
					return emailApi.emailSendLocalHtml(formData.value)
				} else return Promise.reject()
			})
			.then(() => {
				message.success('发送成功')
			})
			.finally(() => {
				emit('loadingEnd')
			})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		send
	})
</script>
